package com.cyy.demo.test.danli;


public class Singleton1 {

    private static class SingletonFactory {
        private static final Singleton1 instance = new Singleton1();
    }

    private Singleton1() {
    }


    public static Singleton1 getInstance() {
        return SingletonFactory.instance;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10; i++) {
            System.out.println(Singleton1.getInstance());
            ;
        }

        for (int i = 0; i < 10; i++) {
            System.out.println(Sinleton2.getInstance());
        }
    }
}

class Sinleton2 {

    private static Sinleton2 sinleton2;

    private Sinleton2() {
    }

    public static Sinleton2 getInstance() {
        if (sinleton2 == null) {
            synchronized (Sinleton2.class) {
                if (sinleton2 == null) {
                    sinleton2 = new Sinleton2();
                }
            }
        }
        return sinleton2;
    }

}
